﻿// Author:  Marco van Wieren <mailto:mvwieren@gmail.com>
// Year:    2012
// License: Microsoft Public License (Ms-PL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mvw.SharePoint.Taxonomy.AutoTagging
{
    public class WorkspaceTermInfoList : List<WorkspaceTermInfo>
    {
        private bool isValid = false;
        public bool IsValid
        {
            get
            {
                return isValid;
            }
        }

        public WorkspaceTermInfoList()
        {
        }

        public WorkspaceTermInfoList(string workspaceTermInfoListAsString)
        {
            if (workspaceTermInfoListAsString == null)
            {
                return;
            }

            if (workspaceTermInfoListAsString.Length == 0)
            {
                return;
            }

            var workspaceTermInfoArr = workspaceTermInfoListAsString.Split(';');
            if (workspaceTermInfoArr.Length % 5 != 0)
            {
                return;
            }

            for (var i = 0; i < workspaceTermInfoArr.Length; i += 5)
            {
                var tmp = new string[5];
                Array.Copy(workspaceTermInfoArr, i, tmp, 0, 5);
                Add(new WorkspaceTermInfo(string.Join(";", tmp)));
            }

            isValid = true;
        }

        public override string ToString()
        {
            var workspaceTermInfoListAsString = new List<string>();
            foreach (var workspaceTermInfo in this)
            {
                workspaceTermInfoListAsString.Add(workspaceTermInfo.ToString());
            }
            return string.Join(";", workspaceTermInfoListAsString.ToArray());
        }
    }
}
